from fastapi import Depends, HTTPException
from starlette.middleware.sessions import SessionMiddleware
from odoo import api, fields, models
from ..handler.exception_handler import odoo_auth_exception_handler
from odoo.addons.web_api.exceptions.http_exception import AuthException
from ..router.order.ship_order_rt import router as ship_order_rt
from ..router.order.login_rt import router as login_rt
from ..router.center_area.compliance_and_agreements_rt import router as compliance_and_agreements_rt
from ..router.center_area.question_and_answer_rt import router as question_and_answer_rt
from ..router.center_area.user_center_rt import router as user_center_rt


class FastapiEndpoint(models.Model):
    _inherit = "fastapi.endpoint"
    app: str = fields.Selection(
        selection_add=[("Web API NEW", "内部接口-网站-新")], ondelete={"Web API NEW": "cascade"})

    def _get_fastapi_routers(self):
        if self.app == "Web API NEW":
            return [ship_order_rt, login_rt, compliance_and_agreements_rt, question_and_answer_rt, user_center_rt]
        return super()._get_fastapi_routers()

    def _get_app(self):
        app = super()._get_app()
        app.add_middleware(SessionMiddleware, secret_key="rectaparcel")
        if self.app == "Web API NEW":
            app.version = "1.0.0"
        return app

    def _get_app_exception_handlers(self):
        handlers = super()._get_app_exception_handlers()
        if self.app == "Web API NEW":
            # 为特定端点添加或覆盖异常处理器
            handlers[AuthException] = odoo_auth_exception_handler
        return handlers
